package com.lianniu.mgr.cms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.lianniu.mgr.base.BaseResult;

import com.lianniu.mgr.cms.dao.IChildNavigationDao;
import com.lianniu.mgr.cms.model.ChildNavigationVo;

import com.lianniu.mgr.cms.service.IChildNavigationService;
import com.lianniu.mgr.constants.ErrorCode;
import com.lianniu.mgr.pagination.PaginationInfo;
import com.lianniu.mgr.util.StringUtil;

@Service
public class ChildNavigationServiceImpl implements IChildNavigationService {

	private final static Logger logger = Logger.getLogger(ChildNavigationServiceImpl.class);
	  
	@Resource
	IChildNavigationDao childNavigationDao;
	
	@Override
	public BaseResult insertChildNavigation(ChildNavigationVo curChildNavigationVo) {
		// TODO Auto-generated method stub
	    logger.info("insertChildNavigation begin");
	    logger.info("insertChildNavigation: input data check begin");
	    logger.info("insertChildNavigation curChildNavigationVo:" +JSON.toJSONString(curChildNavigationVo));	   
	    
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(curChildNavigationVo.getParentTsnId())) {
	      baseResult = BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    } 
	    if (StringUtil.isEmpty(curChildNavigationVo.getxName())) {
	    	   baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	 	      return baseResult;
		  }  
		    
	    if (StringUtil.isEmpty(curChildNavigationVo.getSort())) {
	    	curChildNavigationVo.setSort("0");
		  }   
	    
	    logger.info("insertChildNavigation: input data check end");	    
	    return BaseResult.success(childNavigationDao.insertChildNavigation(curChildNavigationVo));
	}

	@Override
	public BaseResult deleteById(String Id) {
	   // TODO Auto-generated method stub
	   logger.info("deleteById begin");
	   logger.info("deleteById Id:" +Id);
					  
	   return BaseResult.success(childNavigationDao.deleteById(Id));
	}

	@Override
	public BaseResult deleteChildNavigationBatch(String... id) {
		// TODO Auto-generated method stub
	    logger.info("deleteChildNavigationBatch begin");
	    logger.info("deleteChildNavigationBatch Ids:" +id.toString());
	    logger.info("deleteChildNavigationBatch: input data check begin");
	   
	    BaseResult baseResult;
	    if (StringUtil.isEmpty(id.toString())) {
	      baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
	      return baseResult;
	    }
	    logger.info("deleteChildNavigationBatch: input data check end");
	    
	    return BaseResult.success(childNavigationDao.deleteChildNavigationBatch(id));
	}

	@Override
	public BaseResult updateById(ChildNavigationVo curChildNavigationVo) {
		// TODO Auto-generated method stub
	    logger.info("updateById begin");
	    logger.info("updateById: update data check begin");
	    logger.info("updateById curChildNavigationVo:" +JSON.toJSONString(curChildNavigationVo));
	  
	    
	    BaseResult baseResult;
	    
		    if (StringUtil.isEmpty(curChildNavigationVo.getxName())) {
		    	   baseResult =  BaseResult.fail(ErrorCode.ParaCheckErrorRetCode, ErrorCode.ParaCheckErrorRetInfo);
		 	      return baseResult;
			  }  
			    
		    if (StringUtil.isEmpty(curChildNavigationVo.getSort())) {
		    	curChildNavigationVo.setSort("0");
			  }  
	    
	    logger.info("updateById: update data check end");
	    
	    return BaseResult.success(childNavigationDao.updateById(curChildNavigationVo));
	}

	@Override
	public BaseResult selectByChildNavigationForPage(ChildNavigationVo curChildNavigationVo,PaginationInfo paginationInfo) {
		// TODO Auto-generated method stub
	    logger.info("selectByChildNavigationForPage begin.");
	    if (!StringUtil.isEmpty(curChildNavigationVo.getxName())) {
	    	curChildNavigationVo.setxName(curChildNavigationVo.getxName().replace(" ", ""));
	    }
	    logger.info("selectByChildNavigationForPage xName:" +curChildNavigationVo.getxName());
	
	    logger.info("selectByChildNavigationForPage paginationInfo:" +JSON.toJSONString(paginationInfo));
	    
	    curChildNavigationVo.setPaginationInfo(paginationInfo);
	    List<ChildNavigationVo> list=childNavigationDao.selectByChannelForPage(curChildNavigationVo);
	    Map<String,Object> retmap=new HashMap<String,Object>();
	    retmap.put("total", paginationInfo.getTotalRecord());
	    retmap.put("curPage", paginationInfo.getCurPage());
	    retmap.put("rows", list);
	    logger.info("selectByChildNavigationForPage:  end");    
	    
	    return BaseResult.success(retmap); 
	}

	@Override
	public BaseResult selectById(ChildNavigationVo curChildNavigationVo) {
		// TODO Auto-generated method stub
	    logger.info("selectById begin");
	    logger.info("selectById Id:" +curChildNavigationVo.getId());
	  
	    return BaseResult.success(childNavigationDao.selectById(curChildNavigationVo.getId()));
	}

	@Override
	public Boolean checkXName(String xName) {
		// TODO Auto-generated method stub
	    logger.info("checkXName begin");
	    logger.info("checkXName xName:" +xName);
	    logger.info("checkXName: input data check begin");   
	  
	    if (StringUtil.isEmpty(xName)) {       
	      return false;
	    }
	    logger.info("checkXName: input data check end");
	    
	    ChildNavigationVo ChildNavigationVo=new ChildNavigationVo();	   
	    ChildNavigationVo.setxName(xName);
	    int count=childNavigationDao.count(ChildNavigationVo);
	    if(count>0){
	      return false;      
	    }
	    else{
	      return true;
	    }   
	}

}
